package com.edushare.common.vo;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;

import com.edushare.common.constants.DefaultFiles;
import com.edushare.common.util.CryptionUtil;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.FileDirectories.DIRECTORY_OWNER_TYPE;
import com.edushare.common.vo.auto._Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class Users extends _Users implements IFileUploadable, EduShareEntity,
		NameComparision {

	private static final long serialVersionUID = -8658556862045340337L;
	public static final int DELETED = 10;
	public static final int INACTIVE = 20;
	public static final int INVITED = 30;
	public static final int ACTIVE = 40;

	public static final Integer LANDING_PAGE_USER_HOME = 10;
	public static final Integer LANDING_PAGE_USER_INVITATION = 20;
	public static final Integer LANDING_PAGE_UNIVERSITY_SETUP = 30;

	public Users() {
		setStatus(UserRoles.STATUS_INVITED);
		setSettings("zms:qd;zmv:qd;zcv:qd;zcc:qd;mNotify:t;eNotify:t;fNotify:t;pNotify:t;aNotify:t;");
	}

	@Override
	public void setConfKey(String uniqueKey) {
		String timeStampedKey = uniqueKey + "$" + System.currentTimeMillis();
		super.setConfKey(timeStampedKey);
	}

	/**
	 * @return key after encrypting the plain-text key
	 */
	@Override
	public String getConfKey() {
		String cKey = null;
		try {
			cKey = CryptionUtil.encrypt(super.getConfKey());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cKey;
	}

	@Override
	public void setPassword(String plainTxtPwd) {
		setPassword(plainTxtPwd, true);
	}

	public void setPassword(String plainTxtPwd, boolean encryption) {
		String encryptPwd = plainTxtPwd;
		if (encryption)
			encryptPwd = DigestUtils.sha256Hex(plainTxtPwd);
		super.setPassword(encryptPwd);
	}

	/** IFileUploadable implementation starts */
	public String getUploadedFileUrl() {
		if (getImageFile() == null) {
			return DefaultFiles.getDefaultImageFileUrl(this);
		}
		return getImageFile().getFileDownloadUrl();
	}

	public DIRECTORY_OWNER_TYPE getObjectType() {
		return DIRECTORY_OWNER_TYPE.USER;
	}

	public int getObjectPrimaryKey() {
		return getUserId();
	}

	public String getTitle() {
		return getCompleteUserName();
	}

	/** IFileUploadable implementation ends */

	/**
	 * @return FirstName[space]lastName or userName
	 */
	public String getCompleteUserName() {
		String name = "";
		if(getStatus() == INVITED)
			return getEmail();
		name = name
				+ (StringUtil.isValid(getFirstName()) ? getFirstName() + " "
						: "");
		name = name + (StringUtil.isValid(getLastName()) ? getLastName() : "");
		name = name + (StringUtil.isValid(name) ? "" : getUserName());
		List<UserRoles> rL = getRoleList();
		for(UserRoles r:rL){
			if(r.getRoleType() == UserRoles.STUDENT_ROLE && rL.size() == 1){
				if(StringUtil.isValid(r.getUnivUniqId()))
					name += " ("+r.getUnivUniqId()+")";
			}
		}
		if(!StringUtil.isValid(name))
			name = getEmail();
		return name;
	}

	public String getName() {
		return getCompleteUserName();
	}

	public int compareTo(INameComparable o) {
		return getName().compareTo(o.getName());
	}

	public boolean isGroupMember(Groups g) {
		for (GroupMembers gm : getGroupMemberships()) {
			if (gm.getGroup().getObjectId().equals(g.getObjectId())) {
				if (gm.getStatus() == GroupMembers.STATUS_ACTIVE)
					return true;
				else
					return false;
			}
		}
		return false;
	}

	public boolean isGroupAdmin(Groups g) {
		for (GroupMembers gm : getGroupMemberships()) {
			if (gm.getGroup().getObjectId().equals(g.getObjectId())
					&& gm.getRole() == GroupMembers.GROUP_ADMIN) {
				if (gm.getStatus() == GroupMembers.STATUS_ACTIVE)
					return true;
				else
					return false;
			}
		}
		return false;
	}

	public int getGroupRole(Groups g) {
		for (GroupMembers gm : getGroupMemberships()) {
			if (gm.getGroup().getObjectId().equals(g.getObjectId())) {
				if (gm.getStatus() == GroupMembers.STATUS_ACTIVE)
					return gm.getRole();
				else
					return 0;
			}
		}
		return 0;
	}
	
	public int getMembershipStatus(Groups g) {
		for (GroupMembers gm : getGroupMemberships()) {
			if (gm.getGroup().getObjectId().equals(g.getObjectId())) {
				return gm.getStatus();
			}
		}
		return 0;
	}
}